# !/user/bin/env python3
# -*- coding:utf-8 -*-
# import pytest
from selenium import webdriver
import unittest, time
from contrast import ImageCompare
# from ddt import ddt,data
from public import *
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import shutil
import datetime
from logger import init_log_config
import logging
# @ddt
class TestAutomation:

    #  CT拍摄
    def test_01_CT_shooting(self):
        self.IC = ImageCompare()
        # 启动浏览器
        self.driver = webdriver.Chrome()
        self.driver.get('http://127.0.0.1:8189/#/')
        self.driver.maximize_window()
        self.driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入手机号"]').send_keys("19973877211")  # 输入手机号
        self.driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入密码"]').send_keys('111111')  # 输入密码
        self.driver.find_element_by_xpath('//div[@class="zh_postForm"]').click()  # 点击登录
        time.sleep(5)
        self.driver.find_element_by_xpath('//div[text()="登记"]').click()  # 进入登记页
        time.sleep(1)
        patient_name = random_name()
        self.driver.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name))  # 输入患者姓名参数化
        time.sleep(1)
        self.driver.find_element_by_xpath('//div[@class="patients curpo clickImg"]').click() #选择拍摄模式
        self.driver.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()  # 点击下一步
        time.sleep(5)
        self.driver.find_element_by_xpath('//button[@class="ok-btn userSelect bianseBorder"]').click()
        try:
            time.sleep(3)
            progress_locator = (By.XPATH, '//div[contains(text()," 准备完成")]')
            WebDriverWait(self.driver, 90, 1).until(EC.visibility_of_element_located(progress_locator))
            time.sleep(1)
            self.driver.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()
            time.sleep(1)
            shutil.copy('D:\\HandSwitchOn.txt', 'D:\\CTDebug')  # 复制手闸文件到CTDebug目录
            time.sleep(70)  # 等待采集曝光完成
            filepath = 'D:\\ycw\\CTscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
            self.driver.save_screenshot(filepath)
        #     try:
        #         time.sleep(1)
        #         assert (self.IC.calc_similar_by_path('E:\\20200917212239.png', filepath) * 100)
        #     except Exception as e:
        #         logging.info('匹配失败{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        #         raise e
        # except:
        #     error_filepath = 'E:\\CTscreenshot\\error\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        #     self.driver.save_screenshot(error_filepath)
        #     logging.info("{}曝光失败！".format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        finally:
            self.driver.quit()

    # 全景拍摄

    def test_02_Panoramic_shooting(self):
        self.IC = ImageCompare()
        # 启动浏览器
        self.driver = webdriver.Chrome()
        self.driver.get('http://127.0.0.1:8189/#/')
        self.driver.maximize_window()
        self.driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入手机号"]').send_keys("19973877211")
        self.driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入密码"]').send_keys('111111')
        self.driver.find_element_by_xpath('//div[@class="zh_postForm"]').click()
        time.sleep(5)
        self.driver.find_element_by_xpath('//div[text()="登记"]').click()
        time.sleep(1)
        patient_name2 = random_name()
        self.driver.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name2))  # 输入患者姓名参数化
        self.driver.find_element_by_xpath('//div[@class="patients curpo clickImg"]/following-sibling::div[1]').click()
        self.driver.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()
        time.sleep(5)
        self.driver.find_element_by_xpath('//button[@class="ok-btn bianseBorder"]').click()
        time.sleep(3)
        progress_locator = (By.XPATH,'//div[contains(text()," 准备完成")]')
        WebDriverWait(self.driver,30).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        self.driver.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()
        time.sleep(1)
        self.driver.find_element_by_xpath('//div[@class="yg-button-abs1"]//div[3]').click()
        shutil.copy('D:\\HandSwitchOn.txt','D:\\CTDebug')
        time.sleep(60)
        filepath = 'E:\\PXscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        self.driver.save_screenshot(filepath)
        # try:
        #     time.sleep(1)
        #     assert (self.IC.calc_similar_by_path('E:\\20200917215622.png', filepath) * 100)
        # except Exception as e:
        #     logging.info('匹配失败{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        #     raise e
        self.driver.quit()

    # 头侧拍摄

    def test_03_Head_side_shot(self):
        self.IC = ImageCompare()
        # 启动浏览器
        self.driver = webdriver.Chrome()
        self.driver.get('http://127.0.0.1:8189/#/')
        self.driver.maximize_window()
        self.driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入手机号"]').send_keys("19973877211")
        self.driver.find_element_by_xpath('//input[@class="yg-input__inner" and @placeholder="请输入密码"]').send_keys('111111')
        self.driver.find_element_by_xpath('//div[@class="zh_postForm"]').click()
        time.sleep(5)
        self.driver.find_element_by_xpath('//div[text()="登记"]').click()
        time.sleep(1)
        patient_name3 = random_name()
        self.driver.find_element_by_xpath('//input[@placeholder="患者姓名" and @class="yg-input__inner" ]').send_keys("{}".format(patient_name3)) # 输入患者姓名参数化
        self.driver.find_element_by_xpath('//div[@class="patients curpo clickImg"]/following-sibling::div[2]').click()
        self.driver.find_element_by_xpath('//div[@class="check-in-wrap"]/following-sibling::div[1]//div[2]').click()
        time.sleep(5)
        self.driver.find_element_by_xpath('//button[@class="ok-btn m-b40 m-t205 bianseBorder"]').click()
        time.sleep(3)
        progress_locator = (By.XPATH,'//div[contains(text()," 准备完成")]')
        WebDriverWait(self.driver,30).until(EC.visibility_of_element_located(progress_locator))
        time.sleep(1)
        self.driver.find_element_by_xpath('//div[@class="yg-imgtxtbtn text-center"]').click()
        # time.sleep(1)
        # self.driver.find_element_by_xpath('//div[@class="buttonicon-left"]//span[3]/div').click()
        shutil.copy('D:\\HandSwitchOn.txt','D:\\CTDebug')
        time.sleep(60)
        filepath = 'E:\\DXscreenshot\\{}.png'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")))  # 存放在
        self.driver.save_screenshot(filepath)
        # try:
        #     time.sleep(1)
        #     assert (self.IC.calc_similar_by_path('E:\\20200917221458.png', filepath) * 100)
        # except Exception as e:
        #     logging.info('匹配失败{}'.format(str(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))))
        #     raise e
        self.driver.quit()


if __name__ == '__main__':
    for i in range(1):
        # TestAutomation().test_01_CT_shooting()
        # TestAutomation().test_02_Panoramic_shooting()
        TestAutomation().test_03_Head_side_shot()